//
//  DataHandle.m
//  YinYueBoFang_Forever
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "DataHandle.h"
#import "MusicModel.h"

@interface DataHandle()

@property(nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation DataHandle


+(DataHandle *)defaultDataHandle
{
    static dispatch_once_t onceToken;
    static DataHandle *dataHandle = nil;
    dispatch_once(&onceToken, ^{
        dataHandle = [[DataHandle alloc] init];
        dataHandle.dataArray = [NSMutableArray array];
        [dataHandle loadData];
    });
    return dataHandle;
}

-(void)loadData
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *string = @"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist";
        NSURL *url = [NSURL URLWithString:string];
        NSArray *array = [NSArray arrayWithContentsOfURL:url];
        for (NSDictionary *dic in array) {
            MusicModel *model = [[MusicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        //当数据处理完毕，回到主线程发送通知
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"finishData" object:nil];
            
        });
        
    });
}

-(NSInteger)musicModelCount
{
    return self.dataArray.count;
}
-(MusicModel *)musicModelWithindex:(NSInteger)index
{
    return self.dataArray[index];
}













@end
